package com.pzb.wechat.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.pzb.common.config.WechatConfig;
import com.pzb.common.utils.http.HttpUtils;
import com.pzb.wechat.service.IWechatComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pzb.wechat.mapper.WechatAuthorizationMapper;
import com.pzb.wechat.domain.WechatAuthorization;
import com.pzb.wechat.service.IWechatAuthorizationService;

/**
 * 授权信息Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2022-01-04
 */
@Service
public class WechatAuthorizationServiceImpl implements IWechatAuthorizationService
{
    private final String API_URL = "https://api.weixin.qq.com/cgi-bin/component";

    @Autowired
    private WechatAuthorizationMapper wechatAuthorizationMapper;
    @Autowired
    private IWechatComponentService componentService;

    /**
     * 查询授权信息
     * 
     * @param authorizerAppid 授权信息主键
     * @return 授权信息
     */
    @Override
    public WechatAuthorization selectWechatAuthorizationByAuthorizerAppid(String authorizerAppid)
    {
        return wechatAuthorizationMapper.selectWechatAuthorizationByAuthorizerAppid(authorizerAppid);
    }

    /**
     * 查询授权信息列表
     *
     * @return 授权信息
     */
    @Override
    public List<WechatAuthorization> selectWechatAuthorizationList()
    {
        return wechatAuthorizationMapper.selectWechatAuthorizationList();
    }

    /**
     * 新增授权信息
     * 
     * @param wechatAuthorization 授权信息
     * @return 结果
     */
    @Override
    public int insertWechatAuthorization(WechatAuthorization wechatAuthorization)
    {
        return wechatAuthorizationMapper.insertWechatAuthorization(wechatAuthorization);
    }

    /**
     * 修改授权信息
     * 
     * @param wechatAuthorization 授权信息
     * @return 结果
     */
    @Override
    public int updateWechatAuthorization(WechatAuthorization wechatAuthorization)
    {
        return wechatAuthorizationMapper.updateWechatAuthorization(wechatAuthorization);
    }

    @Override
    public String getAuthorizerAccessToken(String authorizerAppid) {
        WechatAuthorization authorization =  selectWechatAuthorizationByAuthorizerAppid(authorizerAppid);
        if(authorization!=null){
            //计算时间差
            Double min = (new Date().getTime()-authorization.getRefreshTime().getTime())/(1000*60)*1.0;
            //如果大于110分钟重新获取
            if(min < 110){
                //return authorization.getAuthorizerAccessToken();
            }
            JSONObject param = new JSONObject();
            param.put("component_appid", WechatConfig.getAppId());
            param.put("authorizer_appid", authorization.getAuthorizerAppid());
            param.put("authorizer_refresh_token", authorization.getAuthorizerRefreshToken());
            String componentAccessToken = componentService.getApiComponentToken();
            String result =  HttpUtils.sendPost(API_URL+"/api_authorizer_token?component_access_token="+componentAccessToken,param.toString());
            JSONObject object = JSONObject.parseObject(result);
            String authorizer_access_token = object.getString("authorizer_access_token");
            authorization.setAuthorizerAccessToken(authorizer_access_token);
            authorization.setRefreshTime(new Date());
            authorization.setAuthorizerRefreshToken(object.getString("authorizer_refresh_token"));
            wechatAuthorizationMapper.updateWechatAuthorization(authorization);
            return authorizer_access_token;
        }
        return null;
    }

}
